package com.ziq.zcamp.community

import android.content.Context
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter
import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.common.BannerResult
import com.ziq.zcamp.bean.life.CampLifeEntranceGroup
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.bean.post.CommunityPostingGroup
import com.ziq.zcamp.bean.post.PostingDetail
import com.ziq.zcamp.post.PostingAdapter

class CommunityPostingAdapter(context: Context, val lifecycle: Lifecycle, val itemWidth:Int) : GroupedRecyclerViewAdapter(context),
    LifecycleObserver{

    companion object {
        const val TYPE_POSTING = 1
        const val TYPE_BANNER = 2
        const val TYPE_ENTRANCE = CampLifeEntranceGroup.TYPE_CAMP_LIFE
    }

    var postingAdapterAction: PostingAdapter.PostingAdapterAction? = null
    fun setAction(postingAdapterAction: PostingAdapter.PostingAdapterAction){
        this.postingAdapterAction = postingAdapterAction
    }

    var groupList: MutableList<DetailGroup> = mutableListOf()
    var postingGroup = CommunityPostingGroup(this)
    var campLifeGroup = CampLifeEntranceGroup(this)

    init {
        groupList.add(campLifeGroup)
        groupList.add(postingGroup)
        lifecycle.addObserver(this)
    }

    fun setData(list: MutableList<PostingDetail>) {
        postingGroup.postingList = list
        postingGroup.hasShowCampLife(!campLifeGroup.list.isNullOrEmpty())
        notifyDataSetChanged()
    }

    fun setCampLifeData(list : MutableList<Int>?){
        //3.21.00 不显示露营人生
//        campLifeGroup.list = list
//        postingGroup.hasShowCampLife(!campLifeGroup.list.isNullOrEmpty())
//        notifyDataSetChanged()
    }

    fun setBannerData(list: MutableList<BannerResult.BannerItem>?) {
        if(list != null ){
            postingGroup.setBannerData(list)
            postingGroup.hasShowCampLife(!campLifeGroup.list.isNullOrEmpty())
            notifyDataSetChanged()
        }
    }

    var isStop = false
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onPause() {
        isStop = true
        try {
            postingGroup.lastBanner?.stopLoop()
        }catch (e:Exception){
            e.printStackTrace()
        }

    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onResume() {
        isStop = false
        try {
            if(postingGroup.lastBanner?.isAttachedToWindow == true){
                postingGroup.lastBanner?.startLoop()
            }
        }catch (e:Exception){
            e.printStackTrace()
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy() {
        isStop = true
        try {
            postingGroup.lastBanner?.stopLoop()
        }catch (e:Exception){
            e.printStackTrace()
        }
    }


    override fun getGroupCount(): Int {
        return groupList.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return groupList.get(groupPosition).getChildrenCount()
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return groupList[groupPosition].hasHeader(groupPosition)
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return groupList[groupPosition].hasFooter(groupPosition)
    }

    override fun getHeaderViewType(groupPosition: Int): Int {
        return groupList[groupPosition].getHeaderViewType(groupPosition)
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return groupList[groupPosition].getChildViewType(groupPosition, childPosition)
    }

    override fun getHeaderLayout(viewType: Int): Int {
        return 0
    }

    override fun getFooterLayout(viewType: Int): Int {
        return 0
    }

    override fun getChildLayout(viewType: Int): Int {
        return when (viewType) {
            TYPE_POSTING -> {
                R.layout.item_community_recommended
            }
            TYPE_BANNER -> {
                R.layout.item_community_banner
            }
            TYPE_ENTRANCE -> {
                R.layout.item_camp_life_entrance
            }
            else -> {
                0
            }
        }
    }

    override fun onBindHeaderViewHolder(holder: BaseViewHolder?, groupPosition: Int) {
        groupList[groupPosition].onBindHeaderViewHolder(holder, groupPosition)
    }

    override fun onBindFooterViewHolder(holder: BaseViewHolder?, groupPosition: Int) {
        groupList[groupPosition].onBindFooterViewHolder(holder, groupPosition)

    }

    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {
        groupList[groupPosition].onBindChildViewHolder( holder, groupPosition, childPosition)
    }
}
